/**
 *
 * ......................我佛慈悲.......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG....................
 *         CSDN博客:http://blog.csdn.net/song5347
 *       github地址:https://github.com/lingsuiderongyan
 *             邮箱:lingsuiderongyan@163.com
 */
//表情视图自定义cell
import UIKit

class LSEmotionCollectionViewCell: UICollectionViewCell {
    
    //MARK: - 表情数组模型
    var emoticons: [LSEmoticon]? {
        didSet {
            guard let ets = emoticons else {
                return
            }
            
            //默认情况下表情按钮都隐藏,设置数据的时候再让他显示出来
            for value in emoticonButtonArray {
                //隐藏表情
                value.hidden = true
            }
            
            //绑定表情数据
            for (i, value) in ets.enumerate() {
                //获取表情按钮控件
                let emoticonButton = emoticonButtonArray[i]
                
                //绑定数据显示表情按钮
                emoticonButton.hidden = false
                
                //绑定表情模型
                emoticonButton.emoticon = value
                
                //如果是0表示设置图片
                if value.type == "0" {
                    emoticonButton.setImage(UIImage(named: value.path!), forState: .Normal)
                    //设置图片不需要设置文字
                    emoticonButton.setTitle(nil, forState: .Normal)
                } else {
                    //设置emoji
                    emoticonButton.setTitle((value.code! as NSString).emoji(), forState: .Normal)
                    //设置emoji不需要设置图片
                    emoticonButton.setImage(nil, forState: .Normal)
                }
            }
        }
    }
    
    //MARK: - 记录常见20个表情按钮
    private lazy var emoticonButtonArray: [LSEmoticonButton] = [LSEmoticonButton]()
    
    var indexPath: NSIndexPath? {
        didSet {
            guard let index = indexPath else {
                return
            }
            messageLabel.text = "当前显示的是第\(index.section + 1)组第\(index.item + 1)页"
        }
    }
    
    //MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = {
        let label = UILabel(fontSize: 20, textColor: UIColor.blackColor())
        
        return label
    }()
    
    //MARK: - 删除表情按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "deleteButtonAction", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //添加表情按钮
        addChildButton()
        //添加删除表情按钮
        contentView.addSubview(deleteButton)
    }
    
    //MARK: - 添加表情按钮
    private func addChildButton() {
        //遍历20次创建表情按钮
        for _ in 0..<20 {
            let button = LSEmoticonButton()
            
            //添加点击事件
            button.addTarget(self, action: "emoticonButtonAction:", forControlEvents: .TouchUpInside)
            
            //设置字体大小
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            
            contentView.addSubview(button)
            
            //存储到数组里面
            emoticonButtonArray.append(button)
        }
    }
    //MARK: - 点击删除按钮事件
    @objc private func deleteButtonAction() {
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedDeleteEmoticonNotification, object: nil)
    }
    
    //MARK: - 点击表情按钮操作
    @objc private func emoticonButtonAction(button: LSEmoticonButton) {
        let emoticon = button.emoticon
        
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedEmoticonNotification, object: emoticon)
    }
    
    //MARK: - 给表情按钮布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算表情按钮的宽度
        let itemWidth = width / 7
        //计算表情按钮的高度
        let itemHeight = height / 3
        
        for (i, value) in emoticonButtonArray.enumerate() {
            value.size = CGSize(width: itemWidth, height: itemHeight)
            
            //计算当前列的索引
            let colIndex = i % 7
            //计算当前行的索引
            let rowIndex = i / 7
            
            //设置x坐标和y坐标
            value.x = CGFloat(colIndex) * itemWidth
            value.y = CGFloat(rowIndex) * itemHeight
        }
        //设置删除按钮的大小
        deleteButton.size = CGSize(width: itemWidth, height: itemHeight)
        //设置xy坐标
        deleteButton.x = width - itemWidth
        deleteButton.y = height - itemHeight
    }
}



















